<?php
namespace app\shops\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;


/**
 * 广告管理
 */
class Banner extends Base
{
    /**
     *
     * @throws
     * 广告列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
      /*  $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];*/
        $pageSize   = input('limit')    ? input('limit')    : 6;

        // banner列表
        $banners = Db("banner")->where("shop_id",$this->shop_id)->where("is_del",0)->where('type',1)->order('id desc')->paginate($pageSize);

        // 查询所有的门店信息
        $store_list = Db("store")->where("shop_id",$this->shop_id)->select();

        // 整理门店信息， store_id => store_name
        if($store_list){
            foreach ($store_list as $key => $val){
                $newStore[$val['store_id']] = $val['store_name'];
            }
        }

        $banner_list = [];
        if($banners){
            foreach ($banners as $k => $v){

                $banner_list[$k] = $v;

                if($v['store_id'] >0){
                    $banner_list[$k]['store_name'] = $newStore[$v['store_id']];
                }else{
                    $banner_list[$k]['store_name'] = "所有门店";
                }

            }
        }


        $this->assign([
            'total'                => $banners->total(),
            'page'                 => $banners->render(),
            'banner_list'          => $banner_list
        ]);

        return $this->fetch();
    }


    /**
     * @throws
     * 广告回收站
     */
    public function index_del()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Model('banner');
        $list = $model->where('is_del',1)->where('type',1)->where('shop_id',$this->shop_id)->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * 添加广告
     */
    public function add()
    {

        if(request()->isPost()){

            // 图片资源
            $file   = request()->file('pic_url');

            if(!$file){
                $this->error("广告图片未上传");
            }

            // 本地路径
            $filePath = $file->getRealPath();

            // 文件后缀
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

            // 七牛存储文件名
            $file_name = 'images/banner/'.date("Ymd").rand(1000,9999).'.'.$ext;

            // 实例化七牛云
            $qiniu = new Qiniu();

            // 上传至七牛云
            $re = $qiniu->upload_pic($filePath,$file_name);
            if($re['code'] == 0){
                $this->error("图片未成功上传七牛云".$re['data']);
            }

            // 存入数据库
            $data['title']      = input("title");
            $data['url']        = input("url",'/');
            $data['pic_url']    = $re["data"];
            $data['shop_id']    = $this->shop_id;
            $data['store_id']   = input('store_id');
            $data['start_time'] = strtotime(input('start_time'));
            $data['end_time']   = strtotime(input('end_time'));
            $data['type']       = 1;

            $ret =Db('banner')->insert($data);

            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }

        //拿到门店列表
        $store_list = Db('store')->field('store_name,store_id')->where('shop_id',$this->shop_id)->select();

        $this->assign('store_list',$store_list);

        return $this->fetch();
    }


    /**
     * @throws
     * 编辑广告
     */
    public function edit()
    {
        $id = input('id');

        // 更新数据
        if(request()->isPost()){

            $data = input('');

            // 图片是否被编辑
            $file   = request()->file('pic_url');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/banner/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }

                $data['pic_url']    = $re["data"];

            }

            input('store_id')       == null || $data['store_id']        = input('store_id');
            input('is_del')         == null || $data['is_del']          = input('is_del');
            input('start_time')     == null || $data['start_time']      = strtotime(input('start_time'));
            input('end_time')       == null || $data['end_time']        = strtotime(input('end_time'));



            $re = Db('banner')->where('id',$id)->update($data);

            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");
        }

        //拿到门店列表
        $store_list = Db('store')->field('store_name,store_id')->where('shop_id',$this->shop_id)->select();

        // 广告详情
        $banner_info = Db('banner')->where('shop_id',$this->shop_id)->where('id',$id)->find();

        $this->assign([
            'store_list'     => $store_list,
            'banner_info'    => $banner_info,
        ]);


        return $this->fetch();
    }


    
}
